#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: syntax.sh
# Description: 
#   
#	 Action Description
#	 p Prints the line
#	 d Deletes the line
#	 s/pattern1/pattern2/ Substitutes the first occurrence of pattern1 with pattern2.
#
#    sed -e 'command1' -e 'command2' ... -e 'commandN' files

#    Terminology:
#     1.Pattern Space
#         a.You can think of 'Pattern Space' as a buffer.
#         b.Each time sed read in a line, it will be placed here for further editing.
#           There is one another 'buffer' named Hold Space
#         c.Usually we keep one line in Pattern Space. sed provides some advanced commands to keep several lines in pattern space where lines are separated by newline characters.
#     2.Cycle
#         Each cycle consists of
#         a.Overwriting Pattern Space with next line.
#         b.Execute commands from top of the script on the Pattern Space.
#         c.If the end of the script (not the data file!) is reached,
#	        print the contents of Pattern Space
#	        (this default action can be disabled by the '-n' option).
#	  3.Hold Space
#	      Another 'buffer', relevant commands are x, h, g, and G
#
# Edit History: 
#   2011-01-15    File created.
#========================================================================
#print all the lines that match the pattern
#This demonstrates the default behavior of sed--it prints every input line to the output. 
sed '/0\.[0-9][0-9]$/p' fruit_prices.txt

#-n To avoid print each line 
sed -n '/0\.[0-9][0-9]$/p' fruit_prices.txt

#the words mango or Mango to be deleted
sed '/^[Mm]ango/d' fruit_prices.txt

#update the file
#mv fruit_prices.txt fruit_prices.txt.$$
#sed '/^[Mm]ango/d' fruit_prices.txt.$$ > fruit_prices.txt
#cat fruit_prices.txt

#To fix the spelling of Paech, Substitutions
sed 's/Paech/Peach/' fruit_prices.txt

#the first misspelling was fixed, but the second one was not.
sed 's/eqal/equal/' nash.txt
#The g (global) operator tells the s command to substitute
#every occurrence of pattern1 with pattern2.
sed 's/eqal/equal/g' nash.txt

#the s command provides us with the & operator, which enables us to reuse the string that
#matched pattern1 in pattern2.
sed 's/ *[0-9][0-9]*\.[0-9][0-9]$/|\$&|/' fruit_prices.txt
#Multiple sed Commands
sed -e 's/Paech/Peach/' -e 's/ *[0-9][0-9]*\.[0-9][0-9]$/\$&/' fruit_prices.txt

#Using sed in a Pipeline
/usr/bin/id | sed 's/(.*$//'
/usr/bin/id | sed -e 's/(.*$//' -e 's/^uid=//'
